package com.javaweb.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaweb.admin.query.FormtableMain12083Query;
import com.javaweb.admin.service.IFormtableMain12083Service;
import com.javaweb.admin.vo.formtablemain12083.FormtableMainQuery;
import com.javaweb.common.annotation.Log;
import com.javaweb.common.common.BaseController;
import com.javaweb.common.enums.LogType;
import com.javaweb.common.service.IUploadService;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.entity.User;
import com.javaweb.system.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/formtable")
public class FormTableController extends BaseController {

    @Autowired
    IFormtableMain12083Service formtableMain12083Service;
    @Autowired
    private IUploadService uploadService;


    @GetMapping("/query/{wxtId}/{wxtbh}")
    //@Log(title = "AF活动管理-劳务协议", logType = LogType.OTHER)
    public JsonResult query(@PathVariable("wxtId") String wxtId, @PathVariable("wxtbh") String wxtbh) {
        FormtableMain12083Query query = new FormtableMain12083Query();
        query.setWxtbh(wxtbh);
        query.setWxtId(wxtId);
        return formtableMain12083Service.getMainList(query);
    }

    @PostMapping("/getOaData/{wxtId}")
    //@Log(title = "AF活动管理-劳务协议", logType = LogType.OTHER)
    public JsonResult getOaData(@PathVariable("wxtId") String wxtId, @RequestBody FormtableMainQuery param) {
        if (StringUtils.isEmpty(wxtId)){
            return JsonResult.error("参数为空");
        }
        return formtableMain12083Service.getOaData(wxtId,param);
    }

    @PostMapping("/submitdata")
    @Log(title = "AF活动管理-劳务协议", logType = LogType.INSERT)
    public JsonResult submitdata(@RequestBody String body) {
        //String body = JSON.toJSONString(entity);
        return formtableMain12083Service.Create(body);
    }

    @PostMapping("/uploadSingleFile/{documentId}")
    public JsonResult uploadSingleFile(@RequestParam("file") MultipartFile file, @PathVariable("documentId") String documentId)  {
        return formtableMain12083Service.uploadFile(file,documentId,"");
    }

    @PostMapping("/submitOaData")
    @Log(title = "OA协议", logType = LogType.INSERT)
    public JsonResult submitOaData(@RequestBody String body) {
        //String body = JSON.toJSONString(entity);
        return formtableMain12083Service.submitOaData(body);
    }

    @PostMapping("/uploadOaFile/{wxtid}/{wxtbh}/{fileid}")
    public JsonResult uploadOaFile(@RequestParam("file") MultipartFile file, @PathVariable("wxtid") String wxtid,@PathVariable("wxtbh") String wxtbh,@PathVariable("fileid") String fileid)  {
        return formtableMain12083Service.uploadOaFile(file,wxtid,wxtbh,fileid);
    }

    @PostMapping("/uploadFile/{docDir}/{wxtId}")
    @Log(title = "AF活动管理-劳务协议附件上传", logType = LogType.INSERT,isSaveRequestData = false)
    public JsonResult uploadFile(@RequestParam("fileHead") MultipartFile[] fileHead,@RequestParam("fileBody") MultipartFile[] fileBody, @PathVariable("docDir") String docDir, @PathVariable("wxtId") String wxtId)  {
        return formtableMain12083Service.uploadMultiFile(fileHead,fileBody,docDir,wxtId);
    }

    @PostMapping("/uploadFileDtl/{wxtbh}/{wxtId}/{dtWxtbh}")
    @Log(title = "AF活动管理-劳务协议附件上传", logType = LogType.INSERT,isSaveRequestData = false)
    public JsonResult uploadFileDt(@RequestParam("fileHead") MultipartFile[] fileHead,@RequestParam("fileBody") MultipartFile[] fileBody, @PathVariable("wxtbh") String wxtbh, @PathVariable("wxtId") String wxtId, @PathVariable("dtWxtbh") String dtWxtbh)  {
        if (StringUtils.isEmpty(dtWxtbh)) {
            return  JsonResult.error("附件外系统编号未上传");
        }
        return formtableMain12083Service.uploadFileDt(fileHead,fileBody,wxtbh,wxtId,dtWxtbh);
    }
    @PostMapping("/importYJ")
    public JsonResult importYJ(@RequestParam("file") MultipartFile file)  {
        return formtableMain12083Service.importYJ(file);
    }

    @PostMapping("/uploadSphSalesFile/{formPk}/{fileId}")
    public JsonResult uploadSphSalesFile(@RequestParam("file") MultipartFile file, @PathVariable("formPk") String formPk,@PathVariable("fileId") String fileId)  {
        String ly = null;
        try {
            User user = ShiroUtils.getUserInfo();
            ly = user.getAvatar();
        } catch (Exception e) {

        }
        if (StringUtils.isEmpty(ly)) {
            return JsonResult.error("请登录");
        }
        if (StringUtils.isEmpty("formPk")|| StringUtils.isEmpty("fileId")) {
            return JsonResult.error("链接不完整");
        }
        return formtableMain12083Service.uploadSphSalesFile(file,formPk,fileId,ly);
    }


    @PostMapping("/submitSphSalesForm/{formType}")
    @Log(title = "OA协议", logType = LogType.INSERT)
    public JsonResult submitSphSalesForm(@RequestBody String body, @PathVariable("formType") String formType) {
        String ly = null;
        try {
            User user = ShiroUtils.getUserInfo();
            ly = user.getAvatar();
        } catch (Exception e) {

        }
        if (StringUtils.isEmpty(ly)||StringUtils.isEmpty(formType)) {
            return JsonResult.error("链接不完整，或登录状态失效重新登录!");
        }
        //String body = JSON.toJSONString(entity);
        return formtableMain12083Service.submitSphSalesForm(formType, body, ly);
    }

    @PostMapping("/querySphSalesForm/{formType}/{formPk}")
    public JsonResult querySphSalesForm(@PathVariable("formType") String formType, @PathVariable("formPk") String formPk) {
        String ly = null;
        try {
            User user = ShiroUtils.getUserInfo();
            ly = user.getAvatar();
        } catch (Exception e) {

        }
        if (StringUtils.isEmpty(ly) || StringUtils.isEmpty(formType)) {
            return JsonResult.error("链接不完整，或登录状态失效重新登录!");
        }
        //String body = JSON.toJSONString(entity);
        return formtableMain12083Service.querySphSalesForm(formType, formPk, ly);
    }

    @PostMapping("/querySphSalesForms/{formType}")
    public JsonResult querySphSalesForms(@RequestBody String body, @PathVariable("formType") String formType) {
        String ly = null;
        try {
            User user = ShiroUtils.getUserInfo();
            ly = user.getAvatar();
        } catch (Exception e) {

        }
        if (StringUtils.isEmpty(ly) || StringUtils.isEmpty(formType)) {
            return JsonResult.error("链接不完整，或登录状态失效重新登录!");
        }
        List<String> list = JSONObject.parseArray(body, String.class);
        if (CollectionUtils.isEmpty(list)) {
            return JsonResult.error();
        }
        return formtableMain12083Service.querySphSalesForms(formType, list, ly);
    }
}
